//
//  NewsViewController.m
//  Blank
//
//  Created by 周星宇 on 15/9/23.
//  Copyright © 2015年 周星宇. All rights reserved.
//

#import "NewsViewController.h"
#import "AFNetWorkingGet.h"
#import "NewsCell.h"
#import "NewsModel.h"
#import <AFNetworking.h>
#import "DOPNavbarMenu.h"
#import "CultureViewController.h"
#import "QuotationViewController.h"
#import "ShoppingViewController.h"
#import "TechnologyViewController.h"
#import "NewsDetailViewController.h"
#import <MJRefresh.h>

@interface NewsViewController () <UITableViewDataSource, UITableViewDelegate, DOPNavbarMenuDelegate>

@property (nonatomic, strong) NSMutableArray *newsArray;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger numberOfItemsInRow;
@property (nonatomic, strong) DOPNavbarMenu *menu;

@end

@implementation NewsViewController

#pragma mark -- 网络请求
- (void)dataHandle{
    self.newsArray = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:MoreNewsURL parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSArray *array = [responseObject objectForKey:@"info"];
        for (NSDictionary *dic in array) {
            NewsModel *newsModel = [NewsModel NewsModelWithDic:dic];
            [self.newsArray addObject:newsModel];
        }
        if (self.newsArray.count > 0) {
            [self.tableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        
    }];
}
#pragma mark -- 上拉刷新下拉加载
- (void)dataHeaderFooter{
    self.tableView.header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        NSMutableArray *arr = [NSMutableArray array];
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:MoreNewsURL parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            NSArray *array = [responseObject objectForKey:@"info"];
            for (NSDictionary *dic in array) {
                NewsModel *newsModel = [NewsModel NewsModelWithDic:dic];
                [arr addObject:newsModel];
            }
            if (self.newsArray.count > 0) {
                [self.newsArray removeAllObjects];
                self.newsArray = arr;
                [self.tableView reloadData];
                [self.tableView.header endRefreshing];
            }
        } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
            
        }];
    }];
    
    __block NSInteger i = 2;
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        NSString *url = [NSString stringWithFormat:@"%@%ld", MoreNewsUpURL, i];
        i++;
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:url parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            NSArray *array = [responseObject objectForKey:@"info"];
            for (NSDictionary *dic in array) {
                NewsModel *newsModel = [NewsModel NewsModelWithDic:dic];
                [self.newsArray addObject:newsModel];
            }
            if (self.newsArray.count > 0) {
                [self.tableView reloadData];
                [self.tableView.footer endRefreshing];
            }
        } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
            
        }];
    }];
}
#pragma mark -- tableView初始化

- (void)tableViewSubViews{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 45 * Height) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = 0;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[NewsCell class] forCellReuseIdentifier:@"news"];
}
#pragma mark -- 右侧按钮
- (DOPNavbarMenu *)menu {
    if (_menu == nil) {
        DOPNavbarMenuItem *Culture = [DOPNavbarMenuItem ItemWithTitle:@"文化传播" icon:[UIImage imageNamed:@"iconfont-wenhuayishu"]];
        DOPNavbarMenuItem *Quotation = [DOPNavbarMenuItem ItemWithTitle:@"行业信息" icon:[UIImage imageNamed:@"iconfont-xingye"]];
        DOPNavbarMenuItem *Shopping = [DOPNavbarMenuItem ItemWithTitle:@"导购评价" icon:[UIImage imageNamed:@"iconfont-daogou"]];
        DOPNavbarMenuItem *Technology = [DOPNavbarMenuItem ItemWithTitle:@"技术专区" icon:[UIImage imageNamed:@"iconfont-jishulei"]];
        _menu = [[DOPNavbarMenu alloc] initWithItems:@[Culture,Quotation,Shopping,Technology] width:self.view.dop_width maximumNumberInRow:_numberOfItemsInRow];
        _menu.backgroundColor = [UIColor blackColor];
        _menu.separatarColor = [UIColor whiteColor];
        _menu.delegate = self;
    }
    return _menu;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.newsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150 * Height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1 * Height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1 * Height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"news"];
    NewsModel *newsModel = [self.newsArray objectAtIndex:indexPath.section];
    cell.newsModel = newsModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsDetailViewController *newsDetail = [[NewsDetailViewController alloc] init];
    NewsModel *newsModel = [self.newsArray objectAtIndex:indexPath.section];
    newsDetail.id = newsModel.id;
    [self.navigationController pushViewController:newsDetail animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.menu) {
        [self.menu dismissWithAnimation:NO];
    }
}

- (void)openMenu:(id)sender{
    self.navigationItem.rightBarButtonItem.enabled = NO;
    if (self.menu.isOpen) {
        [self.menu dismissWithAnimation:YES];
    } else {
        [self.menu showInNavigationController:self.navigationController];
    }
}
#pragma mark -- 右侧按钮显示
- (void)didShowMenu:(DOPNavbarMenu *)menu {
    [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-gengduoMore"]];
    self.navigationItem.rightBarButtonItem.enabled = YES;
}

- (void)didDismissMenu:(DOPNavbarMenu *)menu {
    [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"iconfont-xiangxia"]];
    self.navigationItem.rightBarButtonItem.enabled = YES;
}
#pragma mark -- 右侧按钮选取
- (void)didSelectedMenu:(DOPNavbarMenu *)menu atIndex:(NSInteger)index {
    if ((index + 1) == 1) {
        CultureViewController *culture = [[CultureViewController alloc] init];
        [self.navigationController pushViewController:culture animated:YES];
    }
    if ((index + 1) == 2) {
        QuotationViewController *quotation = [[QuotationViewController alloc] init];
        [self.navigationController pushViewController:quotation animated:YES];
    }
    if ((index + 1) == 3) {
        ShoppingViewController *shopping = [[ShoppingViewController alloc] init];
        [self.navigationController pushViewController:shopping animated:YES];
    }
    if ((index + 1) == 4) {
        TechnologyViewController *technology = [[TechnologyViewController alloc] init];
        [self.navigationController pushViewController:technology animated:YES];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (void)textViewDidChange:(UITextView *)textView {
    self.numberOfItemsInRow = [textView.text integerValue];
    self.menu = nil;
}

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    self.menu = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"更多";
    [self dataHandle];
    [self tableViewSubViews];
    [self dataHeaderFooter];
    self.numberOfItemsInRow = 4;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-xiangxia"] style:UIBarButtonItemStylePlain target:self action:@selector(openMenu:)];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeNightModel) name:@"nightModelChange" object:nil];
}

- (void) changeNightModel
{
    if ([NightModel getColorModel]) {
        self.tableView.backgroundColor = [UIColor blackColor];
        self.tableView.sectionIndexBackgroundColor = [UIColor colorWithRed:57.0f/255 green:79.0f/255 blue:86.0f/255 alpha:1];
        self.tableView.sectionIndexColor = [UIColor greenColor];
        [self.tableView reloadData];
    }
    else{
        
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.sectionIndexBackgroundColor = nil;
        self.tableView.sectionIndexColor =nil;
        [self.tableView reloadData];
    }
}

#pragma mark - 重写弹出抽屉方法
- (void) openOrCloseLeftList
{
    [super openOrCloseLeftList];
    [NightModel changeAppPageCount:3];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
